<?php 
class Country_Model_Country extends Core_Model_Item_Abstract{
    
  public function getGuid(){
     return 'country_country_'.$this->getIdentity();
  }
  
  public function getHref($params = array()){
    $params = array_merge(array(
      'route' => 'country_profile_name',
      //'action' => 'view',
      'id' => $this->country_id,
      'name' => $this->title,
      'reset' => true
    ), $params);
    $route = $params['route'];
    $reset = $params['reset'];
    unset($params['route']);
    unset($params['reset']);
    return Zend_Controller_Front::getInstance()->getRouter()
      ->assemble($params, $route, $reset);
  }
  
   // handle song upload
  public function createSong($file, $params = array())
  {
	if( $file instanceof Zend_Form_Element_File ) {
            $filename = $file->getFileName();
    }
    else if( is_array($file) ) {
      if( !is_uploaded_file($file['tmp_name']) ) {
        throw new Country_Model_Exception('Invalid upload or file too large');
      }
      $filename = $file['name'];
    } else if( is_string($file) ) {
      $filename = $file;
    } else {
      throw new Country_Model_Exception('Invalid upload or file too large');
    }

    // Check file extension
    if( !preg_match('/\.(mp3|m4a|aac|mp4)$/iu', $filename) ) {
      throw new Country_Model_Exception('Invalid file type');
    }

    // upload to storage system
    $params = array_merge(array(
      'type' => 'national_anthem',
      'name' => $filename,
      'parent_type' => 'country',
      'parent_id' => Engine_Api::_()->user()->getViewer()->getIdentity(),
      'user_id' => Engine_Api::_()->user()->getViewer()->getIdentity(),
      'extension' => substr($filename, strrpos($filename, '.') + 1),
    ), $params);

    $song = Engine_Api::_()->storage()->create($file, $params);

    return $song->getIdentity();
  }
  
  public function setPhoto($photo) {

        if( $photo instanceof Zend_Form_Element_File ) {
            $file = $photo->getFileName();
        } else if( is_array($photo) && !empty($photo['tmp_name']) ) {
            $file = $photo['tmp_name'];
        } else if( is_string($photo) && file_exists($photo) ) {
            $file = $photo;
        } else {
            throw new Country_Model_Exception('Invalid argument passed to setPhoto: '.print_r($photo,1));
        }

        $name = basename($file);
        $path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'temporary';
        $params = array(
                'parent_type' => 'country',
                'parent_id' => $this->getIdentity()
        );
        // Save
        $storage = Engine_Api::_()->storage();
        // Resize image (main)
        $image = Engine_Image::factory();
        $image->open($file)
                ->resize(720, 720)
                ->write($path.'/m_'.$name)
                ->destroy();
        // Resize image (profile)
        $image = Engine_Image::factory();
        $image->open($file)
                ->resize(200, 400)
                ->write($path.'/p_'.$name)
                ->destroy();

        // Resize image (normal)
        $image = Engine_Image::factory();
        $image->open($file)
                ->resize(140, 160)
                ->write($path.'/in_'.$name)
                ->destroy();

        // Resize image (icon)
        $image = Engine_Image::factory();
        $image->open($file);

        $size = min($image->height, $image->width);
        $x    = ($image->width - $size) / 2;
        $y    = ($image->height - $size) / 2;

        $image->resample($x, $y, $size, $size, 65, 65)
                ->write($path.'/is_'.$name)
                ->destroy();

        // Store
        $iMain       = $storage->create($path.'/m_'.$name,  $params);
        $iProfile    = $storage->create($path.'/p_'.$name,  $params);
        $iIconNormal = $storage->create($path.'/in_'.$name, $params);
        $iSquare     = $storage->create($path.'/is_'.$name, $params);

        $iMain->bridge($iProfile,    'thumb.profile');
        $iMain->bridge($iIconNormal, 'thumb.normal');
        $iMain->bridge($iSquare,     'thumb.icon');

        // Remove temp files
    @unlink($path.'/p_'.$name);
    @unlink($path.'/m_'.$name);
    @unlink($path.'/in_'.$name);
    @unlink($path.'/is_'.$name);

    

        return $iMain->getIdentity();
    }

    public function isBlock($user){
        return false;
    }
    public function getFLagTag($type = 'thumb.profile')
    {
        if( empty($this->flag_photo_id) ) {
          return null;
        }
    
        $file = Engine_Api::_()->getItemTable('storage_file')->getFile($this->flag_photo_id, $type);
        if( !$file ) {
          return null;
        }
        $safeName = ( $type ? str_replace('.', '_', $type) : 'main' );
        $attribs = array();
        $attribs['class'] = ( isset($attribs['class']) ? $attribs['class'] . ' ' : '' );
        $attribs['class'] .= $safeName . ' ';
        $attribs['class'] .= 'item_photo_' . $this->getType() . ' ';
        $src =  $file->map();
        if( $src )
        {
          // Add auto class and generate
          $attribs['class'] = ( !empty($attribs['class']) ? $attribs['class'].' ' : '' ) . $safeName;
        }  
        $view = Zend_Registry::get('Zend_View');
        $alt = "";	   
        return $view->htmlImage($src, $alt, $attribs);
   }
   
   public function getBigMapTag($type = null){
        if( empty($this->big_map_photo_id) ) {
          return null;
        }
    
        $file = Engine_Api::_()->getItemTable('storage_file')->getFile($this->big_map_photo_id, $type);
        if( !$file ) {
          return null;
        }
        $safeName = ( $type ? str_replace('.', '_', $type) : 'main' );
        $attribs = array();
        $attribs['class'] = ( isset($attribs['class']) ? $attribs['class'] . ' ' : '' );
        $attribs['class'] .= $safeName . ' ';
        $attribs['class'] .= 'item_photo_' . $this->getType() . ' ';
        $src =  $file->map();
        if( $src )
        {
          // Add auto class and generate
          $attribs['class'] = ( !empty($attribs['class']) ? $attribs['class'].' ' : '' ) . $safeName;
        }  
        $view = Zend_Registry::get('Zend_View');
        $alt = "";	   
        return $view->htmlImage($src, $alt, $attribs);
   }
	public function getFlagUrl($type = null)
    {
        $photo_id = $this->flag_photo_id;
        if( !$photo_id ) {
          return null;
        }
    
        $file = Engine_Api::_()->getItemTable('storage_file')->getFile($photo_id, $type);
        if( !$file ) {
          return null;
        }
    
        return $file->map();
  }
  	public function getPhotoUrl($photo_id ,$type = null)
    {
        if( !$photo_id ) {
          return null;
        }
    
        $file = Engine_Api::_()->getItemTable('storage_file')->getFile($photo_id, $type);
        if( !$file ) {
          return null;
        }
    
        return $file->map();
    }
    
	  public function getAnthemPath()
	  {
		$file = Engine_Api::_()->getItem('storage_file', $this->national_anthem_id);
		if( $file ) {
		  return $file->map();
		}
	  }
	
    public function isMember($user_id){ // false is not a member
            
        $countrymemberTb = Engine_Api::_()->getDbTable('countrymembers','country');
        $select = $countrymemberTb->select()
                                    ->where('user_id = ?', $user_id)
                                    ->where('country_id = ?', $this->getIdentity());
                                    
        $rows = $countrymemberTb->fetchAll($select);
        if(count($rows) == 0){
            return false;
        }
        return true;
    }
    public function membership()
    {
        return new Engine_ProxyObject($this, Engine_Api::_()->getDbtable('countrymembers', 'country'));
    }
}
?>